<script setup>
import PageContainer from '@/components/PageContainer.vue'
import { onMounted, ref } from 'vue'
import { artDelChannelService, artGetChannelsService } from '@/api/article.js'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelEdit from '@/views/article/components/ChannelEdit.vue'

const channelList = ref([])

const getChannelList = async () => {
  // 调用获取文章分类的接口获取数据
  const res = await artGetChannelsService()
  // 将获取到的数据进行复制
  channelList.value = res.data.data
  loading.value = false
}
// 在onMounted函数中进行调用
onMounted(() => {
  getChannelList()
})
const loading = ref(true)
const dialog = ref()
const onAddChannel = () => {
  dialog.value.open({})
}
const onEditChannel = (row) => {
  dialog.value.open(row)
}

const onSuccess = () => {
  getChannelList()
}
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  await getChannelList()
}
</script>

<template>
  <!--通过props传递标题-->
  <page-container title="文章分类">
    <!--具名插槽 extra  定制头部右侧额外的按钮-->
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <!--表格有几列写几个el-table-column。type="index"添加序号-->
      <el-table-column label="序号" width="100" type="index"></el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <!-- row就是channelList的每一项 -->
        <template #default="{ row }">
          <!--图标样式为圆形：circle，按钮为漏空的效果：plain -->
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 弹窗,这样我们就可以将之前的弹框代码进行删除 -->
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>